function convtArrToString(inArr) {
	var outString = "";
	for (var key in inArr) {
		var value = inArr[key];
		outString += key + "=" + value + "&";
	}
	outString = outString.substr(0, outString.length - 1);
	return outString;
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function checkMobile(mobile) {
	if (mobile.length < 11) {
		return false;
	}
	return true;
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};
import rules from "@/static/js/formRules.js"
module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	checkMobile: checkMobile,
	convtArrToString: convtArrToString,
	showError(msg) {
		uni.showToast({
			icon: "none",
			title: msg
		});
	},
	showSuccess(msg) {
		uni.showToast({
			icon: "success",
			title: msg
		});
	},
	checkForm(e) {
		const _this = this;
		let status;
		for (let i in e) {
			let callback = _this.checkItem(i, e[i]);
			if (callback.status) {
				status = true;
			} else {
				uni.showToast({
					title: callback.message,
					icon: "none"
				})
				return false;
			}
		}
		return status;
	},
	handlerCheckResult(callback) {
		if (callback.status) {
			return true;
		} else {
			uni.showToast({
				title: callback.message,
				icon: "none"
			})
			return false;
		}
	},
	checkItem(e, value) {
		switch (e) {
			case 'phone':
				if (value) {
					if (rules.phone.test(value)) {
						return {
							status: true
						}
					} else {
						return {
							status: false,
							message: '请输入正确的手机号码'
						}
					}
				} else {
					return {
						status: false,
						message: '请输入手机号码'
					}
				}
				break
			case 'wallet_name':
				if (value) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '请输入钱包名称'
					}
				}
				break
			case 'invite_code':
				if (value) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '请输入邀请码'
					}
				}
				break
			case 'mnemonic':
				console.log("check mnemonic")
				if (value) {
					let valueArr = value.split(" ")
					console.log(valueArr)
					console.log(valueArr.length)
					if (valueArr.length != 12) {
						return {
							status: false,
							message: '助记词由12个单词组成，请检查！'
						}
					} else {
						return {
							status: true
						}
					}
				} else {
					return {
						status: false,
						message: '请输入助记词'
					}
				}
				break
			case 'wallet_address':
				if (value) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '请输入钱包地址'
					}
				}
				break
			case 'amount':
				if (value - 0 > 0) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '请输入数量'
					}
				}
				break
			case 'private_key':
				if (value.length == 64) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '私钥错误！'
					}
				}
				break
			case 'name':
				if (value) {
					if (rules.chinese.test(value)) {
						return {
							status: true
						}
					} else {
						return {
							status: false,
							message: '姓名只能中文'
						}
					}
				} else {
					return {
						status: false,
						message: '请输入姓名'
					}
				}
				break
			case 'password':
				if (value) {
					if (rules.password.test(value)) {
						return {
							status: true
						}
					} else {
						return {
							status: false,
							message: '登录密码6-16位'
						}
					}
				} else {
					return {
						status: false,
						message: '请输入密码'
					}
				}
				break
			case 'trade_password':
				if (value) {
					if (rules.password.test(value)) {
						return {
							status: true
						}
					} else {
						return {
							status: false,
							message: '8-16位，至少含数字/字母/字符2种组合'
						}
					}
				} else {
					return {
						status: false,
						message: '请输入密码'
					}
				}
				break
			case 'phoneCode':
				if (value) {
					return {
						status: true
					}
				} else {
					return {
						status: false,
						message: '请输入手机验证码'
					}
				}
				break
			default:
				return {
					status: true
				}

		}
	},
	is_null(str) {
		if (str == null || str == "" || str == undefined) {
			return true;
		} else {
			return false;
		}
	},
	logout() {
		uni.removeStorageSync("user");
		uni.reLaunch({
			url: "/pages/user/login/login"
		})
	},
	getUser() {
		var user = {};
		user = uni.getStorageSync("user");
		return user;
	},
	getUid() {
		var user = {};
		var that = this;
		user = uni.getStorageSync("user");
		return user != null && typeof(user.uid) != "undefined" ? user.uid : 0
	},
	checkIsVerifyRealName() {
		var that = this;
		var user = uni.getStorageSync("user");
		if (user.name == null || user.name == "") {
			uni.navigateTo({
				url: "/pages/user/realname/realname"
			})
		}
	},
	checkIsSetTradePass() {
		var that = this;
		var user = uni.getStorageSync("user");
		if (user.is_set_tradepass == null || !user.is_set_tradepass) {
			uni.navigateTo({
				url: "/pages/ucenter/setTradePass/setTradePass"
			})
		}
	},
	areadySetTradePass() {
		var that = this;
		var user = uni.getStorageSync("user");
		user.is_set_tradepass = true;
		uni.setStorageSync("user", user);
	},
	getSelectAddressId() {
		var select_address_id = 0;
		select_address_id = uni.getStorageSync("select_address_id");
		return select_address_id;
	},

	setSelectAddressId(select_address_id) {
		uni.setStorageSync("select_address_id", select_address_id);
		return true;
	},

	opensoon() {
		uni.showToast({
			title: "即将开放",
			icon: "none"
		})
	},
	//请求重新加载
	requestRefresh() {
		uni.setStorageSync("reqeustRefreshPage", "yes");
	},
	//是否执行重新加载
	isRequestRefresh() {
		let boolean = uni.getStorageSync("reqeustRefreshPage");
		//console.log("boolean", boolean)
		uni.removeStorageSync("reqeustRefreshPage");
		//console.log("result:", boolean == 'yes')
		return boolean == "yes" ? true : false;
	},
	cutString(str, len) {
		//length属性读出来的汉字长度为1 
		if (str.length * 2 <= len) {
			return str;
		}
		var strlen = 0;
		var s = "";
		for (var i = 0; i < str.length; i++) {
			s = s + str.charAt(i);
			if (str.charCodeAt(i) > 128) {
				strlen = strlen + 2;
				if (strlen >= len) {
					return s.substring(0, s.length - 1) + "...";
				}
			} else {
				strlen = strlen + 1;
				if (strlen >= len) {
					return s.substring(0, s.length - 2) + "...";
				}
			}
		}
		return s;
	}
}
